package edu.bu.grocerylist.dao;

import java.util.List;

import org.mybatis.spring.support.SqlSessionDaoSupport;

import edu.bu.grocerylist.model.Category;
import edu.bu.grocerylist.model.GroceryList;
import edu.bu.grocerylist.model.GroceryListItem;
import edu.bu.grocerylist.model.Item;

/**
 * Implementation of our GroceryListItemDao interface for MyBatis
 * @author donlanp
 *
 */
public class GroceryListItemDao extends SqlSessionDaoSupport implements IGroceryListItemDao {

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#insertGroceryListItem(edu.bu.grocerylist.model.GroceryListItem)
	 */
	public GroceryListItem insertGroceryListItem(GroceryListItem item) {
		getSqlSession().insert("insertListItem", item);
		return item;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#updateGroceryListItem(edu.bu.grocerylist.model.GroceryListItem)
	 */
	public GroceryListItem updateGroceryListItem(GroceryListItem item) {
		getSqlSession().update("updateListItem", item);
		return item;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#deleteGroceryListItem(edu.bu.grocerylist.model.GroceryListItem)
	 */
	public boolean deleteGroceryListItem(GroceryListItem item) {
		int deleted = getSqlSession().delete("deleteListItem", item);
		return deleted == 1;
	}
	
	public void deleteAllGroceryListItemsFromList(GroceryList grocerylist) {
		int grocerylistid = grocerylist.getGroceryListId();
		getSqlSession().delete("deleteAllListItems", grocerylistid);
		
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#getGroceryListItemsByListId(int)
	 */
	public List<GroceryListItem> getGroceryListItemsByListId(int listId) {
		return getSqlSession().selectList("selectListItemsByListId", listId);
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#getGroceryListItemByPrimaryKey(int)
	 */
	public GroceryListItem getGroceryListItemByPrimaryKey(GroceryListItem sample) {
		return getSqlSession().selectOne("selectItemByPK", sample);
	}
	
	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#saveNewCategory(Category)
	 */
	public Category saveNewCategory(Category category) {
		getSqlSession().insert("insertCategory", category);
		return category;
	}
	
	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#saveNewItem(Item)
	 */
	public Item saveNewItem(Item item) {
		getSqlSession().insert("insertItem", item);
		return item;
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#doesItemExist(String)
	 */
	public boolean doesItemExist(String itemName) {
		return null != getSqlSession().selectOne("selectItemByName", itemName);
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#doesCategoryExist(String)
	 */
	public boolean doesCategoryExist(String categoryName) {
		return null != getSqlSession().selectOne("selectCategoryByName", categoryName);
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#getCategoryIdByName(String)
	 */
	public Category getCategoryIdByName(String name) {
		return getSqlSession().selectOne("selectCategoryByName", name);
	}

	/* (non-Javadoc)
	 * @see edu.bu.grocerylist.dao.IGroceryListItemDao#getItemIdByName(String)
	 */
	public Item getItemIdByName(String name) {
		return getSqlSession().selectOne("selectItemByName", name);
	}

}
